"""
    姓名 :  新  哥
    时间 : 2022/6/25 下午 04:51
    文件名 : test01_reg_login.py
    需求：
    
"""
import logging.handlers
from time import sleep

import allure
import pytest
import requests

from API.reg_login_api import RegLoginApi
from common.utils import common_assert, clear_user_info, read_json


@allure.feature("登录注册")
class TestRegLogin(object):
    """注册登录测试用例"""

    def setup_class(self):
        clear_user_info()

    def setup(self):
        # 创建session对象
        session = requests.session()
        # 创建注册登录api的对象
        self.reg_login_api = RegLoginApi(session)

    # 获取图片验证码测试用例
    @allure.story("获取图片验证码测试用例")
    @pytest.mark.parametrize("r,exp_status_code", read_json("reg_login_data", "img_verify_code"))
    def test01_img_verify_code(self, r, exp_status_code):
        """获取图片验证码-小数"""
        # 调用方法
        allure.dynamic.title(r)
        print("获取注册图片验证码成功(r为小数)")
        resp = self.reg_login_api.get_img_verify_code(r)
        # 断言
        assert exp_status_code == resp.status_code

    # 获取短信验证码测试用例
    @allure.story("获取短信验证码测试用例")
    @pytest.mark.parametrize("phone,imgVerifyCode,exp_status_code,exp_status,description",
                             read_json("reg_login_data", "phone_verify_code"))
    def test02_phone_verify_code(self, phone, imgVerifyCode, exp_status_code, exp_status, description):
        """获取注册短信验证码成功(仅必填)"""
        # 调用方法
        allure.dynamic.title(description)
        # 获取图片验证码的cookie
        self.reg_login_api.get_img_verify_code(0.123456789)
        # 获取短信验证码
        resp = self.reg_login_api.get_phone_verify_code(phone, imgVerifyCode)
        # 断言
        common_assert(resp, exp_status_code, exp_status, description)

    # 注册测试用例
    @allure.story("注册测试用例")
    @pytest.mark.parametrize("form_dict,exp_status_code,exp_status,description",
                             read_json("reg_login_data", "user_register"))
    def test03_register(self,form_dict,exp_status_code,exp_status,description):
        """注册成功（仅必填（de_server=on））"""
        allure.dynamic.title(description)
        self.reg_login_api.get_img_verify_code(0.123456789)
        # 获取短信验证码
        self.reg_login_api.get_phone_verify_code(form_dict['phone'], "8888")
        # 注册
        data = form_dict
        resp = self.reg_login_api.user_register(data)
        try:
            common_assert(resp, exp_status_code,exp_status,description)
        except Exception as e:
            logging.error("错误原因{}".format(e))
            raise

    # 登录测试用例
    @allure.story("登录测试用例")
    @pytest.mark.parametrize("mobile,password,exp_status_code,exp_status,description",
                             read_json("reg_login_data", "user_login"))
    def test04_user_login(self,mobile,password,exp_status_code,exp_status,description):
        allure.dynamic.title(description)
        resp = self.reg_login_api.user_login(mobile, password)
        common_assert(resp, exp_status_code, exp_status,description)

    # 登录成功等到60s
    # 装饰器：暂时跳过不执行
    @pytest.mark.skip
    def test05_login_pwd_error_check(self):
        # 密码错误一次
        resp_01 = self.reg_login_api.user_login("13419876880","qqq444")
        # 断言
        common_assert(resp_01,200,100,"密码错误1次")

        # 密码错误二次
        resp_02 = self.reg_login_api.user_login("13419876880","qqq444")
        # 断言
        common_assert(resp_02, 200, 100, "密码错误2次")

        # 密码错误三次
        resp_03 = self.reg_login_api.user_login("13419876880", "qqq444")
        # 断言
        common_assert(resp_03, 200, 100, "账号已被锁定")
        # 等待60s
        sleep(60)
        # 登录成功
        resp_04 = self.reg_login_api.user_login("13419876880", "qqq123")
        # 断言
        common_assert(resp_04, 200, 200, "成功")

